home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / graphics 2d / marquee / marquee.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  4.0 KB  |  162 lines

  1. /*
  2.     File:        marquee.c
  3.  
  4.     Contains:    
  5.  
  6.     Written by: James "im" Beninghaus    
  7.  
  8.     Copyright:    Copyright © 1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/9/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. #include <QuickDraw.h>
  25. #include <Events.h>
  26. #include <Fonts.h>
  27. #include <Menus.h>
  28. #include <Windows.h>
  29. #include <Packages.h>
  30. #include <TextEdit.h>
  31. #include <Dialogs.h>
  32.  
  33. void Resume();
  34. void TrackMarquee(Point start,Rect* resultRect);
  35. void SetMobiusRect(Rect*    rect,short left,short top, short right,short bottom);
  36.  
  37. void Resume() {
  38.     ExitToShell();
  39. }
  40.     
  41. void main () {
  42.     auto    EventRecord        event;
  43.     auto    WindowRecord    window;
  44.     auto    Rect            bounds,
  45.                             rect;
  46.     auto    Point            start;
  47.     
  48.     /*
  49.     ** initialize the macintosh
  50.     */
  51.     InitGraf((Ptr) &qd.thePort);
  52.     InitFonts();
  53.     InitWindows();
  54.     InitMenus();
  55.     TEInit();
  56.     InitDialogs(nil);
  57.     InitCursor();
  58.         
  59.     SetRect(&bounds, 50, 50, 500, 300);
  60.     NewWindow((Ptr)&window, &bounds, "\pApple Computer Inc.", true, noGrowDocProc, (WindowPtr)-1L, false, 0L);
  61.     SetPort((GrafPtr)&window);
  62.     
  63.     SetRect(&rect, 0, 0, 0, 0);
  64.     while (true) {
  65.         GetNextEvent(everyEvent, &event);
  66.         switch (event.what) {
  67.             case mouseDown :
  68.                 start = event.where;
  69.                 GlobalToLocal(&start);
  70.                 TrackMarquee(start, &rect);
  71.                 break;
  72.             case keyDown :
  73.                 ExitToShell();
  74.                 break;
  75.         }
  76.     }
  77. }
  78.  
  79. /*
  80. ** Description
  81. **        TrackMarquee will display a marquee similar to the 
  82. **        selection rectangle tool in MacPaint™. It is assumed that the
  83. **        current port has been set before calling. 
  84. **
  85. ** Parameters
  86. **        start        : the local coordinates where the mouse down occured.
  87. **        resultRect    : the final rectangle that was selected.
  88. */
  89.  
  90. #define    TICKDELAY    2
  91.  
  92. void TrackMarquee(Point start,Rect* resultRect)
  93. {    
  94.     /*
  95.     ** there are fifteen patterns defined here 
  96.     ** each one eight bytes long starting at :
  97.     ** patterns[0], patterns[1], patterns[2], patterns[3],
  98.     ** patterns[4], patterns[5], patterns[6], patterns[7]
  99.     */
  100.     static    unsigned char    patterns[] = {
  101.         0xF8, 0xF1, 0xE3, 0xC7, 0x8F, 
  102.         0x1F, 0x3E, 0x7C, 0xF8, 0xF1, 
  103.         0xE3, 0xC7, 0x8F, 0x1F, 0x3E
  104.     };
  105.     
  106.     auto        Point        mouse;        /* the current mouse location */
  107.     register    short        index;        /* the index of the current patterns array */
  108.     auto        Rect        nowRect,    /* the current rectangle to be framed */
  109.                             thenRect;    /* the last rectangle to be framed */
  110.     auto        long        nowTicks,    /* the current tick count */
  111.                             thenTicks;    /* the last tick count */
  112.     auto        PenState    penState;    /* the saved pen state on entry to procedure */
  113.  
  114.     
  115.     thenTicks = 0;
  116.     index = 0;
  117.     
  118.     GetPenState(&penState);
  119.     PenMode(patXor);
  120.     
  121.     PenPat((Pattern*)&patterns[index]);
  122.     SetRect(&nowRect, start.h, start.v, start.h, start.v);
  123.     FrameRect(&nowRect);
  124.     thenRect = nowRect;
  125.     
  126.     while (StillDown()) {
  127.         nowTicks = TickCount();
  128.         GetMouse(&mouse);
  129.         SetMobiusRect(&nowRect, start.h, start.v, mouse.h, mouse.v);
  130.         if (((thenTicks + TICKDELAY) < nowTicks ? thenTicks = nowTicks, true : false) || 
  131.         (!EqualRect(&nowRect, &thenRect))) {
  132.             FrameRect(&thenRect);
  133.             index = index < 7 ? index + 1 : 0;
  134.             PenPat((Pattern*)&patterns[index]);
  135.             FrameRect(&nowRect);
  136.             thenRect = nowRect;
  137.         }
  138.     }
  139.     FrameRect(&thenRect);
  140.     
  141.     SetPenState(&penState);
  142.     *resultRect = thenRect;
  143. }
  144.  
  145. void SetMobiusRect(Rect*    rect,short left,short top, short right,short bottom)
  146. {
  147.     if (left > right) {
  148.         rect->left = right;
  149.         rect->right = left;
  150.     } else {
  151.         rect->left = left;
  152.         rect->right = right;
  153.     }
  154.     if (top > bottom) {
  155.         rect->top = bottom;
  156.         rect->bottom = top;
  157.     } else {
  158.         rect->top = top;
  159.         rect->bottom = bottom;
  160.     }
  161. }
  162.